/**
 * Created by ChenYong on 2017-04-12.
 *
 * 举报列表。
 */

$(function () {
    /** 举报人的 jQuery 对象 */
    var $reportCreatePerson = $("#report_create_person");
    /** 举报人手机号的 jQuery 对象 */
    var $reportCreatePersonPhone = $("#report_create_person_phone");
    /** 发红包人的 jQuery 对象 */
    var $reportHandlePerson = $("#report_handle_person");
    /** 举报标题 jQuery 对象 */
    var $reportName = $("#report_name");
    /** 商户订单号 jQuery 对象 */
    var $mchBillno = $("#mch_billno");
    /** 红包状态 jQuery 对象 */
    var $status = $("#status");
    /** 红包发送时间的开始日期的 jQuery 对象 */
    var $sendTimeStartDate = $("#send_time_start_date");
    /** 红包发送时间的结束日期的 jQuery 对象 */
    var $sendTimeEndDate = $("#send_time_end_date");
    /** 红包退款时间的开始日期的 jQuery 对象 */
    var $refundTimeStartDate = $("#refund_time_start_date");
    /** 红包退款时间的结束日期的 jQuery 对象 */
    var $refundTimeEndDate = $("#refund_time_end_date");
    /** 搜索按钮的 jQuery 对象 */
    var $searchReportBtn = $("#search_report_btn");
    /** 举报列表的表格，DataTables 的 API 对象 */
    var reportTable = null;

    // 选择红包发送时间的开始日期
    $sendTimeStartDate.datepicker({
        format: "yyyy-mm-dd",
        language: "zh-CN",
        clearBtn: true,
        autoclose: true,
        todayHighlight: true
    }).on("changeDate", function (e) {
        var startDate = ($.isNull(e.date) ? null : e.date);

        $sendTimeEndDate.datepicker("setStartDate", startDate);
    });

    // 选择红包发送时间的结束日期
    $sendTimeEndDate.datepicker({
        format: "yyyy-mm-dd",
        language: "zh-CN",
        clearBtn: true,
        autoclose: true,
        todayHighlight: true
    }).on("changeDate", function (e) {
        var endDate = ($.isNull(e.date) ? null : e.date);

        $sendTimeStartDate.datepicker("setEndDate", endDate);
    });

    // 选择红包退款时间的开始日期
    $refundTimeStartDate.datepicker({
        format: "yyyy-mm-dd",
        language: "zh-CN",
        clearBtn: true,
        autoclose: true,
        todayHighlight: true
    }).on("changeDate", function (e) {
        var startDate = ($.isNull(e.date) ? null : e.date);

        $refundTimeEndDate.datepicker("setStartDate", startDate);
    });

    // 选择红包退款时间的结束日期
    $refundTimeEndDate.datepicker({
        format: "yyyy-mm-dd",
        language: "zh-CN",
        clearBtn: true,
        autoclose: true,
        todayHighlight: true
    }).on("changeDate", function (e) {
        var endDate = ($.isNull(e.date) ? null : e.date);

        $refundTimeStartDate.datepicker("setEndDate", endDate);
    });

    // 监听红包状态下拉列表搜索
    $status.change(function () {
        searchData();
    });

    // 点击搜索按钮
    $searchReportBtn.click(function (e) {
        searchData();
    });

    // 展示举报红包表格
    reportTable = $("#report_table").myDataTable({
        url: "web/redpack/report/list.json",
        searchBtn: $searchReportBtn,
        sScrollY: "450px",
        sScrollX: true,
        // sScrollXInner: "100%",
        aaSorting: [[9, "desc"]],
        aLengthMenu: [10, 30, 50, 100],
        aoColumns: [{
            mData: "checkbox",
            sTitle: "<label class='pos-rel' title='全选'><input type='checkbox' class='ace'><span class='lbl'></span></label>",
            sWidth: "50px",
            sClass: "center",
            bSortable: false,
            bSearchable: false,
            sDefaultContent: "",
            fnCreatedCell: function (cell, cellData, rowData, row, col) {
                var $cell = $(cell);

                $cell.html("<label class='pos-rel'><input type='checkbox' class='ace' data-id='{0}'><span class='lbl'></span></label>".format(rowData.id));
            }
        }, {
            mData: "reportName",
            sName: "reportName",
            sTitle: "举报标题",
            sWidth: "250px"
        }, {
            mData: "reportCreatePerson",
            sName: "reportCreatePerson",
            sTitle: "举报人",
            sWidth: "150px"
        }, {
            mData: "reportCreatePersonPhone",
            sName: "reportCreatePersonPhone",
            sTitle: "举报人手机号",
            sWidth: "150px",
            sDefaultContent: "",
            fnCreatedCell: function (cell, cellData, rowData, row, col) {
                var $cell = $(cell);

                if ($.isNotBlank(rowData.reportCreatePersonPhone)) {
                    $cell.html("<span class='c-red f-s-20'>{0}</span>".format(rowData.reportCreatePersonPhone))
                }
            }
        }, {
            mData: "openid",
            sName: "openid",
            sTitle: "举报人openid",
            sWidth: "250px"
        }, {
            mData: "reportHandlePerson",
            sName: "reportHandlePerson",
            sTitle: "发红包人",
            sWidth: "150px",
            sDefaultContent: "",
            fnCreatedCell: function (cell, cellData, rowData, row, col) {
                var $cell = $(cell);

                if ($.isNotBlank(rowData.reportHandlePerson)) {
                    $cell.html("<span class='c-red f-s-20'>{0}</span>".format(rowData.reportHandlePerson))
                }
            }
        }, {
            mData: "mchBillno",
            sName: "mchBillno",
            sTitle: "商户订单号",
            sWidth: "250px"
        }, {
            mData: "status",
            sName: "status",
            sTitle: "红包状态",
            sWidth: "150px",
            sDefaultContent: "",
            fnCreatedCell: function (cell, cellData, rowData, row, col) {
                var $cell = $(cell);

                // 发放中
                if (rowData.status == "SENDING") {
                    $cell.html("<span class='label label-lg b-c-orange'>发放中</span>");

                    // 已发放待领取
                } else if (rowData.status == "SENT") {
                    $cell.html("<span class='label label-lg b-c-yellow'>已发放待领取</span>");

                    // 发放失败
                } else if (rowData.status == "FAILED") {
                    $cell.html("<span class='label label-lg b-c-red'>发放失败</span>");

                    // 已领取
                } else if (rowData.status == "RECEIVED") {
                    $cell.html("<span class='label label-lg b-c-green'>已领取</span>");

                    // 退款中
                } else if (rowData.status == "RFUND_ING") {
                    $cell.html("<span class='label label-lg b-c-orange'>退款中</span>");

                    // 已退款
                } else if (rowData.status == "REFUND") {
                    $cell.html("<span class='label label-lg b-c-red'>已退款</span>");

                    // 其他
                } else {
                    $cell.html(rowData.status);
                }
            }
        }, {
            mData: "reportMoney",
            sName: "reportMoney",
            sTitle: "红包金额",
            sWidth: "150px",
            sDefaultContent: "",
            fnCreatedCell: function (cell, cellData, rowData, row, col) {
                // 单元格的 jQuery 对象
                var $cell = $(cell);

                if ($.isNotBlank(rowData.totalAmount)) {
                    $cell.html("<span class='c-red m-r-5'>{0}</span>元".format(rowData.totalAmount))
                }
            }
        }, {
            mData: "sendTime",
            sName: "sendTime",
            sTitle: "红包发送时间",
            sWidth: "150px"
        }, {
            mData: "refundTime",
            sName: "refundTime",
            sTitle: "红包退款时间",
            sWidth: "150px"
        }, {
            mData: "operator",
            sTitle: "操作",
            sWidth: "50px",
            bSortable: false,
            bSearchable: false,
            sDefaultContent: "<div class='hidden-sm hidden-xs action-buttons'></div>"
        }],
        successFn: function () {
            $("[data-toggle='tooltip']").tooltip();
        }
    });

    /**
     *
     * 搜索数据。
     *
     */
    function searchData() {
        // 举报标题
        if ($.isNotBlank($reportName.val()) && ($reportName.val().length > 500)) {
            $.toast({
                top: 5,
                msg: "举报标题的长度不能大于500个字",
                type: "error",
                close: 3000
            });

            return;
        }

        // 举报人
        if ($.isNotBlank($reportCreatePerson.val()) && ($reportCreatePerson.val().length > 50)) {
            $.toast({
                top: 5,
                msg: "举报人的长度不能大于50个字",
                type: "error",
                close: 3000
            });

            return;
        }

        // 举报人手机号
        if ($.isNotBlank($reportCreatePersonPhone.val()) && !phoneRE.test($reportCreatePersonPhone.val())) {
            $.toast({
                top: 5,
                msg: "举报人手机号格式不正确",
                type: "error",
                close: 3000
            });

            return;
        }

        // 发红包人
        if ($.isNotBlank($reportHandlePerson.val()) && ($reportHandlePerson.val().length > 50)) {
            $.toast({
                top: 5,
                msg: "发红包人的长度不能大于50个字",
                type: "error",
                close: 3000
            });

            return;
        }

        // 商户订单号
        if ($.isNotBlank($mchBillno.val()) && (!numberRE.test($mchBillno.val()) || ($mchBillno.val().length != 28))) {
            $.toast({
                top: 5,
                msg: "商户订单号由28个数字组成",
                type: "error",
                close: 3000
            });

            return;
        }

        $reportName.val($.trim($reportName.val()));
        $reportHandlePerson.val($.trim($reportHandlePerson.val()));
        $reportCreatePerson.val($.trim($reportCreatePerson.val()));
        $reportCreatePersonPhone.val($.trim($reportCreatePersonPhone.val()));

        // 搜索开始…
        if ($.isNotNull(reportTable)) {
            reportTable.myAjaxData = {
                status: $status.val(),
                mchBillno: $mchBillno.val(),
                reportName: $reportName.val(),
                reportHandlePerson: $reportHandlePerson.val(),
                reportCreatePerson: $reportCreatePerson.val(),
                reportCreatePersonPhone: $reportCreatePersonPhone.val(),
                sendTimeStartDate: $sendTimeStartDate.val(),
                sendTimeEndDate: $sendTimeEndDate.val(),
                refundTimeStartDate: $refundTimeStartDate.val(),
                refundTimeEndDate: $refundTimeEndDate.val()
            };

            // 刷新举报表格（分页重置）
            reportTable.ajax.reload();
        }
    }
});